// pages/user/login.js
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userName: "",
    password: "",
    checkUser: false,
    checkPass: false,
    Rule: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },
  /**
     * 监听用户
     */
  listernerUser: function (e) {
    var valueUser = e.detail.value;
    this.setData({
      userRule: '',
      checkUser: true,
      userName: valueUser,
    })
  },
  listernerPass: function (e) {
    var valuePass = e.detail.value;
    this.setData({
      passRule: '',
      checkPass: true,
      password: valuePass
    })
  },
  /**
   * 登录
   */
  goLogin: function () {
    var that=this;
    if (this.data.userName == undefined || this.data.userName=="") {
      wx.showToast({
        title: "用户名不能为空",
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (this.data.password == undefined || this.data.password == "") {
      wx.showToast({
        title: "密码不能为空",
        icon: 'none',
        duration: 2000
      })
      return;
    }

    if (this.data.checkUser == true && this.data.checkPass == true && this.data.userName != undefined) {
      console.info("账号", this.data.userName);
      console.info("密码", this.data.password);

      wx.request({
        url: app.globalData.Url +"Api/User/Login", //仅为示例，并非真实的接口地址
        data: {
          name: this.data.userName,
          password: this.data.password
        },
        header: { 'content-type': 'application/json' },
        success: function (res) {
          console.log(res.data);
          if (res.data.result)
          {
            var token = res.data.data.Token;
            console.log(token);
            app.globalData.userInfo = res.data.data;
            app.globalData.Token = token;
           
            try {
              wx.setStorageSync('Token', token)
              wx.setStorageSync('User', app.globalData.userInfo)
            } catch (e) {
            }

            console.log("login Token :" + app.globalData.Token);
            wx.reLaunch({
              url: '/pages/home/index'
            })
          }  
          else{
            wx.showToast({
              title: res.data.data,
              icon: 'none',
              duration: 2000
            })
          }      
        }
      })
    } 
  }
})